<template>
	<view>
		<view>
			<view class="header" style="border-bottom: 1rpx solid lightgrey;">
				<view>
					<image style="height: 150rpx;width: 150rpx;border-radius: 100%;"
						src="/src/static/images/chonwuchang.png" mode="aspectFill"></image>

					<view class="tips" style="color: #000000;">宠物仓</view>
				</view>
			</view>
			<view>
				<view class="getItem">
					登录后宠物仓将获得以下权限
					<view class="item">• 获得你的公开信息（昵称、头像、地区
						等）</view>
				</view>
			</view>
			<!-- #ifdef MP-WEIXIN || H5 -->
			<view @click="getUserInfo" class="buttonIndex">
				<button class="login" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">确认登录</button>
			</view>
			<!-- #endif -->
		</view>

	</view>
</template>

<script setup>
  // 在页面中导入相关 api
  import {
    getRoomInfo,
    weixinMinAppLogin,
    userget
  } from '@/api/index'
  import {
    onLoad
  } from '@dcloudio/uni-app'


	const getPhoneNumber = (e) => {
	    console.log('授权用户手机号')
	    var that = this
	    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
	      wx.showToast({
	        title: '已取消授权',
	      })
	    }
	    if (e.detail.errMsg == 'getPhoneNumber:ok') {
	      wx.login({
	        success: async (res) => {
	          if (res.code != null) {
	            let info = await weixinMinAppLogin({
	              phoneCode: e.detail.code,
	              loginCode: res.code,
	            })
	            console.log(info)

	            if (info.code == 0) {
	              if (info.data) {
	                getuserinfo();

	                //缓存服务器返回的用户信息
	                wx.setStorageSync('token', info.data)
	                wx.showToast({
	                  title: '登录成功',
	                  icon: 'none',
	                })
	                setTimeout(function() {

	                  // getUserDesc(info.data.userId)
	                  // getUserOrder(info.data.userId)

                    uni.reLaunch({
                      url:"/pages/reserve/index"
                    })

	                }, 2000)

	              }
	            }
	          } else {
	            console.log('登录失败！' + res.errMsg)
	          }
	        },
	      })
	    } else {
	      console.log(e.detail.errMsg)
	    }
	  }


    const getuserinfo = async () => {
      var that = this;
      if (uni.getStorageSync("token")) {
        let info = await userget()

        console.log("111", info);
        usertype.value = info.data.userType
      }
    }

</script>

<style>
	@import url("./login.css");
</style>
